Remember?
Register
Back

Templating

By Monkeymatt last edited on Saturday, May 20, 2006 at 1:11:02 pm by Monkeymatt. Page 7.

<< Prev123456789Next >>

Here is the function to output:

PHP Code

<?php
    
// Gets the template ready to be printed
    
public function output() {
        
// go through the bottom of the level_tracker and start the recursive set_array_for_print function
        
foreach ($this->level_tracker[0] as $key=>&$value) {
            if (
$key != '****code****') { // only do it if not ****code**** (all others are real sections)
                
$this->set_array_for_print(&$value$key, &$this->level_tracker[0]);
            }
        }
        
$this->printed=true// set printed variable to say that it was printed
        
return $this->level_tracker[0]['****code****']; // return compiled code
    
}
?>




which requires an addition of this to the global variable list:

Code

public $printed=false; // Whether the template has been printed already (for destructor)


This function goes through the bottom of the $code['base'] array and sends all the section arrays to the set_array_for_print() function (below). It finally sets the $printed variable to true to show that it has been printed, and returns the compiled code. Here is the set_array_for_print():

PHP Code

<?php
    
// Go through arrays and get them ready to print
    // $array is the current array, $key is the name of the section, and $prev_array is the previous array (for setting code)
    
private function set_array_for_print(array &$array$key, array &$prev_array) {
        
$final=''// final code for the array
        
for ($i=0;$i<=$array['****current_iteration****'];$i++) { // go through all iterations of the array
            
if (!empty($array[$i])) {
                if (
is_array($array[$i])) {
                    foreach (
$array[$i] as $key2=>&$value) { // inside 0-9 array keys
                        
if ($key2 != '****code****') { // don't want to mess up ****code****
                            
$this->set_array_for_print(&$value$key2, &$array[$i]); // setup lower sections
                        
}
                    }
                    
$final.=$array[$i]['****code****']; // setup from previous $this->set_array_for_print()
                
}
            }
        }
        
// Replace section code in $code array with the final values for the loop
        
$prev_array['****code****']=preg_replace("|<{".$key."}>(.*)<{/".$key."}>|iUs"$final$prev_array['****code****']);
    }
?>




Here we take an input of $array, which is the one we get data out of, $key, which is the section name, and $prev_array, which is the array we will set the final code into. Note the array before the variables. This is type-hinting. It makes PHP force the user to input an array, and will throw an error if it is not an array.

In this function, we run through all iterations of the loop, and add the code from that section to the final code for the entire loop, going through any further embedded sections as we come across them inside the individual iterations, sending them through the function again. Finally we take the final code, and replace the section reference in the main code with the final value for the loop.

And finally, a helpful (not necessary) function that uses the __destruct() special function to print out the output when the object is destroyed (in this case when the page is done processing):

PHP Code

<?php
    
// Destructs and prints out item
    
public function __destruct() {
        if (!
$this->printed) { // if not printed
            
echo $this->output(); // echo output
        
}
    }
?>




Here we simply check if it has been printed already with the output function, and then if it has not, we simply echo the output.

We have successfully made a templating system. The next pages will have the complete code and examples of how to use it.

<< Prev123456789Next >>